define(['jquery', 'bootstrap', 'userend', 'table', 'form'], function ($, undefined, Userend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/order/myorder/index',
                    table: 'mall_order_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'orderNo', title: __('Orderno')},
                        {field: 'orderunique', title: __('Orderunique'), visible:false},
                        {field: 'shop_id', title: __('Shop_id'), visible:false},
                        {field: 'user_id', title: __('User_id'), visible:false},
                        {field: 'goodsMoney', title: __('Goodsmoney'), operate:'BETWEEN'},
                        {field: 'deliverType', title: __('Delivertype'), searchList: {"0":__('Delivertype 0'),"1":__('Delivertype 1')}, formatter: Table.api.formatter.normal},
                        {field: 'deliverMoney', title: __('Delivermoney'), operate:'BETWEEN'},
                        {field: 'totalMoney', title: __('Totalmoney'), operate:'BETWEEN'},
                        {field: 'realTotalMoney', title: __('Realtotalmoney'), operate:'BETWEEN'},
                        {field: 'payType', title: __('Paytype'),searchList: {"0":__('Paytype 0'),"1":__('Paytype 1')}, formatter: Table.api.formatter.normal},
                        {field: 'payFrom', title: __('Payfrom'), visible:false},
                        {field: 'payis', title: __('Payis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'userName', title: __('Username'), visible:false},
                        {field: 'area_ids', title: __('Area_ids'), visible:false},
                        {field: 'userAddress', title: __('Useraddress'), visible:false},
                        {field: 'userPhone', title: __('Userphone'), visible:false},
                        {field: 'orderScore', title: __('Orderscore'), visible:false},
                        {field: 'invoiceis', title: __('Invoiceis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'orderRemarks', title: __('Orderremarks'), visible:false},
                        {field: 'orderSrc', title: __('Ordersrc'), visible:false},
                        {field: 'needPay', title: __('Needpay'), operate:'BETWEEN', visible:false},
                        {field: 'refundis', title: __('Refundis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'appraiseis', title: __('Appraiseis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'cancelReason', title: __('Cancelreason'), searchList: {"1":__('Cancelreason 1'),"2":__('Cancelreason 2'),"3":__('Cancelreason 3'),"4":__('Cancelreason 4'),"5":__('Cancelreason 5')}, formatter: Table.api.formatter.normal, visible:false},
                        {field: 'rejectReason', title: __('Rejectreason'), searchList: {"1":__('Rejectreason 1'),"2":__('Rejectreason 2'),"3":__('Rejectreason 3'),"99":__('Rejectreason 99')}, formatter: Table.api.formatter.normal, visible:false},
                        {field: 'rejectOtherReason', title: __('Rejectotherreason'), visible:false},
                        {field: 'closedis', title: __('Closedis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'receivetime', title: __('Receivetime'), operate:'RANGE', addclass:'datetimerange', visible:false},
                        {field: 'deliverytime', title: __('Deliverytime'), operate:'RANGE', addclass:'datetimerange', visible:false},
                        {field: 'express_id', title: __('Express_id'), visible:false},
                        {field: 'expressNo', title: __('Expressno'), visible:false},
                        {field: 'tradeNo', title: __('Tradeno'), visible:false},
                        {field: 'settlement_id', title: __('Settlement_id'), visible:false},
                        {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN', visible:false},
                        {field: 'prom_type', title: __('Prom_type'), searchList: {"0":__('Prom_type 0'),"4":__('Prom_type 4'),"5":__('Prom_type 5'),"6":__('Prom_type 6')}, formatter: Table.api.formatter.normal},
                        {field: 'deleted', title: __('Deleted'), searchList: {"1":__('Deleted 1'),"0":__('Deleted 0')}, formatter: Table.api.formatter.normal, visible:false},
                        {field: 'status', title: __('Status'), searchList: {"unpay":__('Unpay'),"undeliver":__('undeliver'),"unreceive":__('Unreceive'),"rejected":__('Rejected'),"finished":__('Finished'),"canceled":__('Canceled'),"unappraise":__('Unappraise')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('Detail'),
                                classname: 'btn btn-xs btn-info btn-fields btn-dialog',
                                icon: 'fa fa-list',
                                url: 'mall/order/order/detail'
                            },{
                                name: 'pay',
                                text: __('Pay'),
                                classname: 'btn btn-xs btn-warning btn-pay',
                                extend: "target='_blank'",
                                icon: 'fa fa-list-ul',
                                visible: function( row){
                                    return  row.status=="unpay";
                                },
                                url: '/addons/mall/order/onlinePayCode/orderNo/{orderunique}'
                            },{
                                name: 'notice',
                                text: __('Notice'),
                                classname: 'btn btn-xs btn-info btn-ajax',
                                icon: 'fa fa-magic',
                                visible: function( row){
                                    //未发货可以提醒
                                    return  row.status=="undeliver" && row.noticeDeliver==0;
                                },
                                confirm: '您确定要提醒商家发货吗?',
                                url: 'mall/order/myorder/notice',
                                refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                            },{
                                name: 'cancel',
                                text: __('Cancel'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    //未发货可以取消
                                    return  row.status=="undeliver" || row.status=="unpay";
                                },
                                url: 'mall/order/myorder/cancel',
                                refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                            },{
                                name: 'receive',
                                text: __('Receive'),
                                classname: 'btn btn-xs btn-success btn-ajax',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    //已发货未收货可以收货
                                    return  row.status=="unreceive";
                                },
                                confirm: '您确定收到此货吗？',
                                url: 'mall/order/myorder/receive',
                                refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                            },{
                                name: 'reject',
                                text: __('Reject'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    //已发货未收货可以拒收
                                    return  row.status=="unreceive";
                                },
                                url: 'mall/order/myorder/reject',
                                refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                            },{
                                name: 'appraise',
                                text: __('Appraise'),
                                classname: 'btn btn-xs btn-success btn-ajax',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    //已发货未收货可以拒收
                                    return  row.status=="unappraise";
                                },
                                url: 'mall/order/myorder/appraise',
                                success: function (data, ret) {
                                    $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click");
                                    $.each(ret.data,function(index,object){
                                        url = ret.url+"/ids/"+object;
                                        Fast.api.open(url, __('Edit'), {ids:object});
                                    });
                                    return false;
                                },
                                error: function (data, ret) {
                                    location.href = ret.url;
                                    return false;
                                }
                            },{
                                name: 'refund',
                                text: __('Refund'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    return  row.allowRefund;
                                },
                                url: 'mall/order/myrefund/add/order_id/{ids}',
                                refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                            },{
                                name: 'complain',
                                text: __('Complain'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    return  row.isComplain;
                                },
                                url: 'mall/order/mycomplain/add/order_id/{ids}',
                                refresh: $($('a[data-toggle="tab"]').attr("href")).find(".btn-refresh").trigger("click")
                            }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        reject: function () {
            Controller.api.bindevent();
            //两个元素互相关联
            $("#c-rejectReason").on("change", function () {
                if($("#c-rejectReason").val()==99){
                    $(".frm-rejectOtherReason").removeClass("hidden");
                    $("#c-rejectOtherReason").focus();
                }
                else{
                    $(".frm-rejectOtherReason").addClass("hidden");

                };
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});